package org.jeecg.modules.lab.sample.handle.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.lab.sample.entity.LabSampleHandle;
import org.jeecg.modules.lab.sample.entity.LabSampleHandleList;
import org.jeecg.modules.lab.sample.handle.mapper.LabSampleHandleListMapper;
import org.jeecg.modules.lab.sample.handle.mapper.LabSampleHandleMapper;
import org.jeecg.modules.lab.sample.handle.service.ILabSampleHandleService;
import org.jeecg.modules.lab.sample.record.service.ILabSampleRecordService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 样品处置表
 * @Author: jeecg-boot
 * @Date:   2023-08-09
 * @Version: V1.0
 */
@Service
public class LabSampleHandleServiceImpl extends ServiceImpl<LabSampleHandleMapper, LabSampleHandle> implements ILabSampleHandleService {

	@Autowired
	private LabSampleHandleMapper labSampleHandleMapper;
	@Autowired
	private LabSampleHandleListMapper labSampleHandleListMapper;

	@Autowired
	private ILabSampleRecordService labSampleRecordService;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(LabSampleHandle labSampleHandle, List<LabSampleHandleList> labSampleHandleListList) {
		labSampleHandleMapper.insert(labSampleHandle);
		if(labSampleHandleListList!=null && labSampleHandleListList.size()>0) {
			for(LabSampleHandleList entity:labSampleHandleListList) {
				//外键设置
				entity.setHandleId(labSampleHandle.getId());
				labSampleHandleListMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(LabSampleHandle labSampleHandle,List<LabSampleHandleList> labSampleHandleListList) {
		labSampleHandle.setSynFlag("1");
		labSampleHandleMapper.updateById(labSampleHandle);
		
		//1.先删除子表数据
		labSampleHandleListMapper.deleteByMainId(labSampleHandle.getId());
		
		//2.子表数据重新插入
		if(labSampleHandleListList!=null && labSampleHandleListList.size()>0) {
			LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			for(LabSampleHandleList entity:labSampleHandleListList) {
				//外键设置
				entity.setHandleId(labSampleHandle.getId());
				labSampleHandleListMapper.insert(entity);
				//样品处置
				labSampleRecordService.addHandRecords(entity,loginUser);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		labSampleHandleListMapper.deleteByMainId(id);
		labSampleHandleMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			labSampleHandleListMapper.deleteByMainId(id.toString());
			labSampleHandleMapper.deleteById(id);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void submitOrCancelHandle(String id, String auditMark) {
		labSampleHandleMapper.updateSampleHandle(id,auditMark);
	}

}
